/*
 * Copyright 2012 yura.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.opu.pcm.ui.client;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.*;
import org.opu.pc.services.beans.AbstractBean;
import org.opu.pc.ui.client.BeanView;
import org.opu.pc.ui.client.MainViewImpl;
import org.opu.pc.ui.client.actions.AbstractAction;
import org.opu.pc.ui.client.actions.Exit;
import org.opu.pc.ui.client.widgets.TableRender;
import org.opu.pc.ui.util.JProperties;
import org.opu.pc.ui.util.ResourceHandle;
import org.opu.pcm.services.beans.UserBean;
import org.opu.pcm.ui.client.actions.CloseUsersSettings;

/**
 *
 * @author yura
 */
public class UserView extends MainViewImpl{

    public static final Class<?>[] TYPES = new Class<?>[]{
        String.class, // name
        String.class, // e-mail
        String.class  // rights
    };
	
    public static final Object[] COLUMN_NAMES = new Object[]{
        "Name", "E-mail", "Rights"
    };

    public UserView(BeanView beanView) {
        super(beanView, new ArrayList<TableRender<?>>(Arrays.asList(
                new TableRender<UserBean>() {

            @Override
            public Class<UserBean> getRenderingClass() {
                return UserBean.class;
            }

            @Override
            public void rendView(Collection<? extends AbstractBean> beans, BeanView view) {
                Object[][] rowData = new Object[beans.size()][TYPES.length];

                int i = 0;
                for (AbstractBean tb : beans) {
                    UserBean bean = (UserBean) tb;
                    rowData[i][0] = bean.getName();
                    rowData[i][1] = bean.getEmail();
                    rowData[i][2] = bean.getSecurityGroups();//.toString();
                    i++;
                }
                view.setContent(COLUMN_NAMES, TYPES, rowData);
            }
        })));
    }
    
    
    @Override
    public void initActions(Collection<? extends AbstractAction> actions) {
        Map<String, AbstractAction> map = new HashMap<String, AbstractAction>();
        for (AbstractAction action : actions) {
            map.put(action.getName(), action);
        }

        final CloseUsersSettings exit = (CloseUsersSettings) map.get(
                CloseUsersSettings.MNAME);

        addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                exit.actionPerformed(null);
            }
        });

        menuBar.initMenuBar(map, ResourceHandle.loadProperties(new JProperties(), 
                "/org/opu/pcm/resource/UsersSettingsMenu.properties"));
        toolBar.initComponent(map, (JProperties)ResourceHandle.loadProperties(new JProperties(), 
                "/org/opu/pcm/resource/UsersSettingsToolBar.properties"));

        setJMenuBar(menuBar);
        getContentPane().add(toolBar, BorderLayout.NORTH);
    }

    @Override
    public void dispose() {
        justDispose();
    }

    @Override
    public void setVisible(boolean b) {
        super.justSetVisible(b);
    }
}
